package com.ykw.parking.controller;

import com.ykw.parking.Utils.Md5;
import com.ykw.parking.mapper.UserMapper;
import com.ykw.parking.pojo1.ParkingLot;
import com.ykw.parking.pojo1.User;
import com.ykw.parking.pojo1.UserLoan;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.security.Security;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Controller
public class HomePageController {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    UserMapper userMapper;
    @RequestMapping(value = "/homePage",method = RequestMethod.POST)
    public String homepage(String  email, String password, Model model, HttpSession session){
        //获取当前的用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户的登录数据
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(email,Md5.code(password));
        try {
            List<ParkingLot> parkingLotRecommend = userMapper.getParkingLotRecommend();
            session.setAttribute("RecommendOne",parkingLotRecommend.get(0));
            session.setAttribute("RecommendTwo",parkingLotRecommend.get(1));
            session.setAttribute("RecommendThree",parkingLotRecommend.get(2));
            session.setAttribute("RecommendFour",parkingLotRecommend.get(3));
            System.out.println("Recommend="+parkingLotRecommend);
            subject.login(usernamePasswordToken);//执行登录方法,如果没有异常就说明ok
            session.setAttribute("username",userMapper.queryUserbyemail(email).getName());
            System.out.println(email+" "+password);
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(new Date());
            redisTemplate.opsForValue().increment("/index.html");
            SimpleDateFormat sdf=new SimpleDateFormat("MM-dd");
            java.util.Date date=new java.util.Date();
            String str=sdf.format(date);
            redisTemplate.opsForHash().increment("visit",str,1);
            return "redirect:/main.html";
        } catch (UnknownAccountException e) {//用户名错误
            model.addAttribute("msg","用户名错误");
            return "login";
        }catch (IncorrectCredentialsException e){
            model.addAttribute("msg","密码错误");
            return "login";
        }

    }
    @RequestMapping("/tologin")
    public String tologin(){

        return "login";
    }
//    @RequestMapping("/homePage")
//    public String homePage(){
//        return "homePage";
//    }
    @RequestMapping("/shirotologin")
    public String tologin1(Model model){
        model.addAttribute("msg","请先登录");
        return "login";
    }
    @RequestMapping("/register")
    public String register(){
        return "register";
    }
}
